package com.jiuyin.dianjing.util;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.HelperApplication;


/**
 * 进度条管理工具
 */
public class ProgressDialogUtil {
    private Dialog mDialog;
    private boolean mCancelable = true;

    /**
     * 显示ProgressDialog
     */
    public void showProgress() {
        showProgress(HelperApplication.getContext());
    }

    /**
     * 显示ProgressDialog
     */
    public void showProgress(Context context) {
        showProgress(context, context.getResources().getString(R.string.progress_tips_def), null);
    }

    /**
     * 显示ProgressDialog
     */
    public void showProgress(Context context, DialogInterface.OnCancelListener listener) {
        showProgress(context, context.getResources().getString(R.string.progress_tips_def), listener);
    }

    /**
     * 显示ProgressDialog
     */
    public void showProgress(Context context, boolean cancelable, DialogInterface.OnCancelListener listener) {
        mCancelable = cancelable;
        showProgress(context, context.getResources().getString(R.string.progress_tips_def), listener);
    }

    /**
     * 显示ProgressDialog
     */
    public void showProgress(Context context, String msg, DialogInterface.OnCancelListener listener) {
        try {
            if (mDialog == null) {
                mDialog = show(context, msg, listener);
            } else if (!mDialog.isShowing()) {
                mDialog.setCanceledOnTouchOutside(mCancelable);
                mDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Dialog show(Context context, String message, DialogInterface.OnCancelListener listener) {

        return ProgressDialog.show(context, null, message, true, mCancelable, listener);
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    /**
     * 取消ProgressDialog
     */
    public void dismissProgress() {
        try {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
